package com.pansoft.openplanet.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.utils.LXGlideImageLoader;
import com.efounder.recycleviewhelper.CommonAdapter;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.WalletBean;
import com.pansoft.openplanet.constant.IPAssetsConstant;

import java.util.ArrayList;
import java.util.List;

public class BottomReceiveAddressChooseDialogFragment extends BottomSheetDialogFragment {

    private static final String TAG = "BottomReceiveAddressChooseDialogFragment";
    private Context mContext;
    private List<WalletBean> list = new ArrayList<>();
    private WalletAdapter walletAdapter;
    private int mode;

    private OnWalletSelectChangedListener onWalletSelectChangedListener;

    public BottomReceiveAddressChooseDialogFragment() {
    }


    @Override
    public void onStart() {
        super.onStart();
//        getDialog().getWindow()
//                .setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
//        getDialog().getWindow().setGravity(Gravity.BOTTOM);
//        getDialog().setCanceledOnTouchOutside(true);
//        getDialog().setCancelable(true);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.open_planet_wallet_choose_popup, container, false);
        initView(rootView);
        return rootView;
    }

    private void initView(View contentView) {
        TextView tvTitle = contentView.findViewById(R.id.title);
        tvTitle.setText("选择收款地址");
        RecyclerView recyclerView = contentView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        walletAdapter = new WalletAdapter(mContext, R.layout.open_planet_item_wallet, list);
        recyclerView.setAdapter(walletAdapter);
        if (mode == IPAssetsConstant.CHAIN_ID_BTC) {

        }else {

        }
    }

    public void setData(Context context, List<WalletBean> list, int mode) {
        this.mContext = context;
        this.list = list;
        this.mode = mode;
    }


    public class WalletAdapter extends CommonAdapter<WalletBean> {

        public WalletAdapter(Context context, int layoutId, List<WalletBean> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(com.efounder.recycleviewhelper.base.ViewHolder holder, final WalletBean walletBean, int position) {

            ImageView mIvWallet;
            TextView mTvAssetName;
            TextView mTvBalance;
            ImageView mIvCheck;

            mIvWallet = (ImageView) holder.getView(R.id.iv_wallet);
            mTvAssetName = (TextView) holder.getView(R.id.tv_asset_name);
            mTvBalance = (TextView) holder.getView(R.id.tv_balance);
            mIvCheck = (ImageView) holder.getView(R.id.iv_check);

            if (mode == IPAssetsConstant.CHAIN_ID_BTC) {
                if (walletBean.getAvatar() == null || walletBean.getAvatar().equals("")) {
                    LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, mIvWallet,
                            R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
                } else {
                    LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, mIvWallet,
                            walletBean.getAvatar(), R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
                }
            } else {
                if (walletBean.getAvatar() == null || walletBean.getAvatar().equals("")) {
                    LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, mIvWallet,
                            R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
                } else {
                    LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, mIvWallet,
                            walletBean.getAvatar(), R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
                }
            }

            mTvAssetName.setText(walletBean.getAccountName());
            String unit;
            switch (mode) {
                case IPAssetsConstant.CHAIN_ID_BTC:
                    unit = "BTC";
                    break;
                case IPAssetsConstant.CHAIN_ID_ETH:
                    unit = "ETH";
                    break;
                case IPAssetsConstant.CHAIN_ID_EOS:
                    case IPAssetsConstant.CHAIN_ID_EOSTEST:
                    unit = "EOS";
                    break;
                default:
                    unit = "ETH";
                    break;
            }
            mTvBalance.setText(walletBean.getAddress());
            if (walletBean.isSelected()) {
                mIvCheck.setVisibility(View.VISIBLE);
            } else {
                mIvCheck.setVisibility(View.GONE);
            }

            holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onWalletSelectChangedListener != null) {
                        for (WalletBean walletBean1 : list) {
                            walletBean1.setSelected(false);
                        }
                        walletBean.setSelected(true);
                        walletAdapter.notifyDataSetChanged();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                onWalletSelectChangedListener.onWalletChanged(walletBean);
                                dismiss();
                                onWalletSelectChangedListener = null;
                            }
                        }, 500);

                    }
                }
            });
        }
    }

    public interface OnWalletSelectChangedListener {
        void onWalletChanged(WalletBean walletBean);
    }

    public void setOnWalletSelectChangedListener(OnWalletSelectChangedListener onWalletSelectChangedListener) {
        this.onWalletSelectChangedListener = onWalletSelectChangedListener;
    }

}
